#Copyright 2009-2010 by Ward, David J ( pymethius@ominian.net )

import logging
log = logging.getLogger(__name__)

from twisted.internet import defer
import twisted.python.failure
from twisted.cred.credentials import UsernamePassword

from webmud.handlers import base 

class Auth(base.Base):
    """
        Hybrid self-directing class that handles the actual login work
    """
    def bootstrap(self, message):
        log.info("Bootstrap called")
        self.sendRequest("loader", "auth")        
    
    def login(self, id, user, password):
        log.debug("auth.login called with %s:%s " % ( user, password, ))
        d = self.realm.login(user, self.protocol, password = password )
        
        d.addCallback(self._login_pass, message_id = id)
            
    def _login_fail(self, avatarInfo, id, *ar, **kw):        
        log.debug("Login failed")
        self.sendResponse(False, id)        
    
    def _login_pass(self, userObj, message_id):
        
        del(self.user) #This should be interesting
        del(self.environ["user"])
        
        self.environ["user"] = userObj        
        self.user = self.environ["user"]
        
        log.debug("Login success %s with Id %s"%  (userObj, message_id,))
        self.sendResponse(True, message_id)
        self.sendRequest("auth.kill", "Ready2roll")            
        #Mark this instance as ready to die
        self.CLEANOUT = True
        
    
    
        

        
    